/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.score.service;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.score.entity.ExchangeRecord;
import com.boxin.ims.modules.wechat.score.dao.ExchangeRecordDao;
import com.boxin.ims.modules.wechat.service.UserDefineService;

/**
 * 积分对换记录Service
 * @author Jakemanse
 * @version 2013-09-23
 */
@Component
@Transactional(readOnly = true)
public class ExchangeRecordService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ExchangeRecordService.class);
	
	@Autowired
	private ExchangeRecordDao exchangeRecordDao;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	@Autowired
	private UserDefineService userDefineService;
	
	public ExchangeRecord get(Long id) {
		return exchangeRecordDao.findOne(id);
	}
	
	public Page<ExchangeRecord> find(Page<ExchangeRecord> page, ExchangeRecord exchangeRecord) {
		DetachedCriteria dc = exchangeRecordDao.createDetachedCriteria();
		dc.createAlias("member", "member");
		if (exchangeRecord.getUser()!=null && !exchangeRecord.getUser().isAdmin()){
			dc.add(Restrictions.eq("user.id", exchangeRecord.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(exchangeRecord.getName())){
			dc.add(Restrictions.like("name", "%"+exchangeRecord.getName()+"%"));
		}
		//手机
		if (exchangeRecord.getMember() != null && StringUtils.isNotEmpty(exchangeRecord.getMember().getMobile())){
			dc.add(Restrictions.like("member.mobile", "%"+exchangeRecord.getMember().getMobile()+"%"));
		}
		if (exchangeRecord.getMember() != null &&  exchangeRecord.getMember().getScore() !=null){
			dc.add(Restrictions.eq("member.score",  exchangeRecord.getMember().getScore() ));
		}
		//状态
		if (StringUtils.isNotEmpty(exchangeRecord.getStatus())){
			dc.add(Restrictions.eq("status", exchangeRecord.getStatus()));
		}
		if (StringUtils.isNotEmpty(exchangeRecord.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+exchangeRecord.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", ExchangeRecord.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return exchangeRecordDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ExchangeRecord exchangeRecord) {
		
		int width = QRCodeUtils.WIDTH;
		int height =QRCodeUtils.HEIGHT;
//		UserDefine userDefine  = userDefineService.getByUserId(exchangeRecord.getUser().getId());
//		if(userDefine!=null){
//			if(userDefine.getQrcodeHeight()!=null){
//				height = userDefine.getQrcodeHeight();
//			}
//			if(userDefine.getQrcodeWidth()!=null){
//				width = userDefine.getQrcodeWidth();
//			}
//		}
		
		//生成二维码
		QRCode qrcode = new QRCode();
		String content = QRCodeUtils.generateScoreExchageSN(exchangeRecord.getUser().getId());
		qrcode.setContent(content);
		String path = Constants.getWechatResourceSavePath();
		path += DateUtils.getTodayString()+File.separator+content;
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, width,height);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
		qrCodeService.save(qrcode);	//保存二维码				
				
		exchangeRecord.setQrCode(qrcode);
		exchangeRecordDao.save(exchangeRecord);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		exchangeRecordDao.deleteById(id);
	}
	
	@Transactional(readOnly = false)
	public void updateStatusById(Long id,String status) {
		exchangeRecordDao.updateStatusById(id,status);
	}
	
}
